<?php


namespace App\Http\Model\Job;


use App\Enum\ClassListPayResultHistoryEnum;
use Illuminate\Support\Facades\DB;

class BankEntryDataVerifyModel
{

    /**
     * 当日收/退款款总额
     *
     * @param $data
     *
     * @return string[]
     */
    public static function fundTotal($data)
    {
        $fundTotal = ['get_fund_total' => '0', 'refund_fund_total' => '0', 'bank_get_money' => '0'];
        $list = DB::select("SELECT `pay_amt`,`refund_amt`,`result` FROM `class_list_pay_result_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `create_date`=:create_date", [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'create_date' => $data['create_date'],
        ]);
        if (empty($list)) {
            return $fundTotal;
        }
        foreach ($list as $val) {
            /*银行实收金额*/
            if ($val->result === ClassListPayResultHistoryEnum::RESULT_SUCCESS) {
                /*当日收款总额*/
                $fundTotal['get_fund_total'] = bcadd($fundTotal['get_fund_total'], $val->pay_amt);
                /*当日退款总额*/
                $fundTotal['refund_fund_total'] = bcadd($fundTotal['refund_fund_total'], $val->refund_amt);
                //$fundTotal['bank_get_money'] = bcadd($fundTotal['bank_get_money'], $val->pay_amt, 2);
            }
        }
        unset($list);
        /*银行监管户动账查询记录表*/
        $list = DB::select('SELECT `tran_dir`,`tran_amt` FROM `pfs_query_host_detail` WHERE `pfs_dw_id`=:pfs_dw_id AND `tran_date`=:tran_date AND `tran_status`=:tran_status', [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            /*清算日期*/
            'tran_date'   => $data['create_date'],
            /*交易状态，固定为S（成功）*/
            'tran_status' => 'S',
        ]);
        if (!empty($list)) {
            foreach ($list as $val) {
                /*
                 * C：贷； D借
                 * 累加借总金额
                 * 累减贷总金额
                 * */
                if ($val->tran_dir === 'C') {
                    $fundTotal['bank_get_money'] = bcsub($fundTotal['bank_get_money'],$val->tran_amt);
                }
                if ($val->tran_dir === 'D') {
                    $fundTotal['bank_get_money'] = bcadd($fundTotal['bank_get_money'],$val->tran_amt);
                }
            }
        }
        return $fundTotal;
    }


    /**
     * 更新记录
     *
     * @param $data
     *
     * @return bool
     */
    public static function save($data, $fundTotal)
    {
        /*查询数据是否已经存在*/
        $exist = DB::select('SELECT `id` FROM `bank_entrydata_verify` WHERE `pfs_dw_id`=:pfs_dw_id AND `transaction_date`=:transaction_date LIMIT 1', [
            'pfs_dw_id'        => $data['pfs_dw_id'],
            'transaction_date' => $data['create_date'],
        ]);
        /*当日应转监管户金额*/
        $spinSuperviseMoney = bcsub($fundTotal['get_fund_total'], $fundTotal['refund_fund_total']);
        $arr = [
            /*招行&数仓-监管账户主键ID*/
            'pfs_dw_id'            => $data['pfs_dw_id'],
            /*监管账号*/
            'account'              => $data['pfs_agencyid'],
            /*主体ID*/
            'ledger_subject_id'    => $data['ledger_subject_id'],
            /*主体名称*/
            'ledger_subject_name'  => $data['ledger_subject_name'],
            /*城市名称*/
            'city_name'            => $data['city_name'],
            /*交易时间(汇总时间)*/
            'transaction_date'     => $data['create_date'],
            /*当日收款总额*/
            'get_fund_total'       => $fundTotal['get_fund_total'],
            /*当日退款总额*/
            'refund_fund_total'    => $fundTotal['refund_fund_total'],
            /*当日应转监管户金额*/
            'spin_supervise_money' => $spinSuperviseMoney,
            /*银行实收金额*/
            'bank_get_money'       => $fundTotal['bank_get_money'],
            /*银行实收日期*/
            'bank_get_date'        => date('Y-m-d'),
            /*差异金额*/
            'diff_money'           => bcsub($spinSuperviseMoney, $fundTotal['bank_get_money']),
            'utime'                => date('Y-m-d H:i:s'),
        ];
        /*记录不存在,则新增*/
        if (empty($exist)) {
            return DB::table('bank_entrydata_verify')->insert($arr);
        }
        /*更新记录*/
        return DB::table('bank_entrydata_verify')->where('id', $exist[0]->id)->update($arr);
    }


    /**
     * 需要处理的业务线-监管账户
     *
     * @return array
     */
    public static function listPsfDw()
    {
        $list = DB::select('SELECT * FROM `pfs_dw_ini` WHERE `is_del`=0');
        $data = [];
        foreach ($list as $val) {
            $data[$val->id] = $val;
        }
        return $data;
    }

}





/**
 * BankEntryDataVerifyModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/11 18:41:00   操作:创建
 **/
